/**
 * @Author: chancoki chhj727@163.com
 * @Date: 2025-05-26 10:21:30
 * @LastEditors: chancoki chhj727@163.com
 * @LastEditTime: 2025-05-26 14:38:45
 * @Copyright (c) 2025 by chancoki, All Rights Reserved.
 **/
const { execFile } = require("child_process");
const path = require("path");

// 获取nircmd.exe的绝对路径
const nircmdPath = process.pkg ? path.join(process.cwd(), "nircmd.exe") : path.join(__dirname, "nircmd.exe");

// 检查nircmd.exe是否存在
const fs = require("fs");
if (!fs.existsSync(nircmdPath)) {
  console.error(`错误: 找不到 nircmd.exe，请确保它与程序在同一目录下`);
  process.exit(1);
}

// 从命令行参数获取设备名称
const deviceName = process.argv[2];

// 检查是否提供了设备名称
if (!deviceName) {
  console.error("请提供音频设备名称作为参数！");
  console.log('使用方法: node set_sound_device.js "设备名称"');
  console.log('示例: node set_sound_device.js "DELL S2421HSX"');
  process.exit(1);
}

// 执行设置默认音频设备命令
execFile(nircmdPath, ["setdefaultsounddevice", deviceName], (error) => {
  if (error) {
    console.error(`执行命令出错: ${error.message}`);
    return;
  }

  // 显示通知
  execFile(nircmdPath, ["trayballoon", "音频输出设备：", `${deviceName} 已设置为默认音频设备`, "shell32.dll,16", "1"], (error) => {
    if (error) {
      console.error(`显示通知出错: ${error.message}`);
    }
  });

  console.log(`已将 ${deviceName} 设置为默认音频设备`);
});
